package cifg;

import com.iid.flappybird.R;


import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;

import cifgu.SKINADATPEW;

import java.lang.ref.WeakReference;

public class GAMEACT extends Activity implements GAMEVIEW.MessageListener {
    private GAMEVIEW gameView;
    private RelativeLayout rl_gameParent;
    private TextView tv_curScore;
    private String curScore;
    private TextView tv_tips;

    private ObjectAnimator objectAnimator;

    private static final int MSG_SCORE_CHANGE = 0x01;
    private static final int MSG_GAME_OVER = 0x02;
    private WeakHandler handler;

    private static class WeakHandler extends Handler {
        private final WeakReference<Activity> mWeakReference;

        public WeakHandler(GAMEACT activity) {
            this.mWeakReference = new WeakReference<>(activity);
        }

        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            GAMEACT activity = (GAMEACT) mWeakReference.get();
            if (activity != null) {
                if (msg.what == MSG_SCORE_CHANGE){
                    activity.changeCurScore(msg.arg1);
                }else if (msg.what == MSG_GAME_OVER){
                    activity.showMessage(msg.arg1);
                }
            }
        }
    }

    @SuppressLint("SetTextI18n")
    private void changeCurScore(int score){
        if (tv_curScore != null) {
            tv_curScore.setText(curScore + score);
        }
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.content_view);
        rl_gameParent = findViewById(R.id.rl_gameParent);
        tv_curScore = findViewById(R.id.tv_curScore);
        tv_tips = findViewById(R.id.tv_tips);
        gameView = new GAMEVIEW(this);
        gameView.setGameBirdActivity(this);
        gameView.setMessageListener(this);
        rl_gameParent.addView(gameView);
        curScore = getString(R.string.CurrentScore);
        handler = new WeakHandler(this);
        changeCurScore(0);
        showAnim();
    }

    @SuppressLint("ObjectAnimatorBinding")
    private void showAnim(){
        objectAnimator = ObjectAnimator.ofFloat(tv_tips, "alpha", 0.5f, 1.0f);
        objectAnimator.setDuration(800); // 设置动画持续时间为1秒
        objectAnimator.setRepeatCount(ValueAnimator.INFINITE); // 设置动画重复次数为无限
        objectAnimator.setRepeatMode(ValueAnimator.REVERSE); // 设置动画重复模式为反向（即透明度从0到1再从1到0循环）
        objectAnimator.start();
    }

    @Override
    protected void onStart() {
        super.onStart();
    }

    public void showMessage(int level) {
        saveSettingData(level);
        onBackPressed();
    }


    private void saveSettingData(int level) {
        SharedPreferences gb_settings = getSharedPreferences(
                SKINADATPEW.GameBirdSettingsFile, 0);
        gb_settings.edit().putInt(SKINADATPEW.Settings_LevelLast, level).apply();
        int top = gb_settings.getInt(SKINADATPEW.Settings_LevelTop, 0);
        if (level > top) {
            gb_settings.edit().putInt(SKINADATPEW.Settings_LevelTop, level).apply();
        }
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void scoreChange(int score) {
        Message message = new Message();
        message.what = MSG_SCORE_CHANGE;
        message.arg1 = score;
        handler.sendMessage(message);
    }

    @Override
    public void gameOver(int score) {
        Message message = new Message();
        message.what = MSG_GAME_OVER;
        message.arg1 = score;
        handler.sendMessage(message);
    }

    @Override
    public void gameStart() {
        if (objectAnimator != null){
            objectAnimator.cancel();
            objectAnimator = null;
            tv_tips.setVisibility(View.GONE);
        }
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        if (gameView != null){
            gameView.Destroy();
        }
        finish();
    }
}